package top.wardyan.wdev.dbassist.config;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import top.wardyan.base.config.Datasource;
import top.wardyan.conf.Const;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;

public class Config {

	protected Shell shell;
	private String name = "数据源编辑";
	private Text txtAddress;
	private Text txtPort;
	private Text txtDBName;
	private Text txtUsername;
	private Text txtPassword;
	private Text txtPwd;
	private Text txtName;
	 

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Config window = new Config();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.isDisposed() && !display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		// shell.setSize(1024, 720);
		shell.setSize(Const.POINT1024);
		//shell.setLocation(0, 0);
		// DDA.SYSTEMCONFIG;
		// 使用SQLITE,用来配置应用程序
		shell.setText(DDA.SYSTEMCONFIG.SystemName + "" + DDA.SYSTEMCONFIG.BuildVersion + " " + name);
		ShellUtil.center(shell);
		
		Label lblTop = new Label(shell, SWT.NONE);
		//lblTop.setFont(SWTResourceManager.getFont("宋体", 36, SWT.NORMAL));
		lblTop.setFont(Const.FONT36);
		lblTop.setAlignment(SWT.CENTER);
		lblTop.setBounds(10, 10, 1006, 60);
		lblTop.setText("数据源编辑");
		
		Label lblAddress = new Label(shell, SWT.NONE);
		lblAddress.setAlignment(SWT.RIGHT);
		lblAddress.setBounds(328, 186, 100, 20);
		lblAddress.setText("地址");
		
		Label lblPort = new Label(shell, SWT.NONE);
		lblPort.setText("端口");
		lblPort.setAlignment(SWT.RIGHT);
		lblPort.setBounds(328, 217, 100, 20);
		
		Label lblDBName = new Label(shell, SWT.NONE);
		lblDBName.setText("数据库名");
		lblDBName.setAlignment(SWT.RIGHT);
		lblDBName.setBounds(328, 243, 100, 20);
		
		Label lbnUsername = new Label(shell, SWT.NONE);
		lbnUsername.setText("数据库用户名");
		lbnUsername.setAlignment(SWT.RIGHT);
		lbnUsername.setBounds(328, 272, 100, 20);
		
		Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setText("数据库密码");
		lblPassword.setAlignment(SWT.RIGHT);
		lblPassword.setBounds(328, 300, 100, 20);
		
		txtAddress = new Text(shell, SWT.BORDER);
		txtAddress.setTextLimit(100);
		txtAddress.setBounds(434, 185, 230, 23);
		
		txtPort = new Text(shell, SWT.BORDER);
		txtPort.setTextLimit(6);
		txtPort.setBounds(434, 214, 230, 23);
		
		txtDBName = new Text(shell, SWT.BORDER);
		txtDBName.setTextLimit(100);
		txtDBName.setBounds(434, 243, 230, 23);
		
		txtUsername = new Text(shell, SWT.BORDER);
		txtUsername.setTextLimit(100);
		txtUsername.setBounds(434, 272, 230, 23);
		
		txtPassword = new Text(shell, SWT.BORDER);
		txtPassword.setTextLimit(100);
		txtPassword.setBounds(434, 300, 230, 23);
		
		Button btnSave = new Button(shell, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = txtName.getText();
				String pwd = txtPwd.getText();
				String address = txtAddress.getText();
				Integer port = Integer.valueOf(txtPort.getText());
				String dbName = txtDBName.getText();
				String username = txtUsername.getText();
				String password = txtPassword.getText();
				Datasource d = new Datasource().setAddress(address).setPort(port).setDbName(dbName).setUsername(username).setPassword(password).setName(name).setPwd(pwd);
				d.write(DDA.DATASOURCEFILE);
				Msg.show(shell, "操作成功", "已成功保存,自动关闭");
				System.exit(0);
			}
		});
		btnSave.setBounds(577, 349, 87, 34);
		btnSave.setText("保存");
		
		Button btnLoad = new Button(shell, SWT.NONE);
		btnLoad.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String p = (String) new PasswordDialog(shell, SWT.NONE).open();
				Datasource d = Datasource.parse(DDA.DATASOURCEFILE);
				if(d.getPwd().equals(p)) {
					txtName.setText(d.getName());
					txtPwd.setText(d.getPwd());
					txtAddress.setText(d.getAddress());
					txtPort.setText(d.getPort().toString());
					txtDBName.setText(d.getDbName());
					txtUsername.setText(d.getUsername());
					txtPassword.setText(d.getPassword());
				}else {
					txtName.setText("");
					txtPwd.setText("");
					txtAddress.setText("");
					txtPort.setText("");
					txtDBName.setText("");
					txtUsername.setText("");
					txtPassword.setText("");
					Msg.show(shell, "密码错误", "密码错误");
					txtName.setFocus();
				}
			}
		});
		btnLoad.setText("载入");
		btnLoad.setBounds(484, 349, 87, 34);
		
		Label lblPwd = new Label(shell, SWT.NONE);
		lblPwd.setText("密码");
		lblPwd.setAlignment(SWT.RIGHT);
		lblPwd.setBounds(328, 158, 100, 20);
		
		txtPwd = new Text(shell, SWT.BORDER);
		txtPwd.setToolTipText("密码为空则不使用密码保护");
		txtPwd.setTextLimit(100);
		txtPwd.setBounds(434, 157, 230, 23);
		
		Label lblName = new Label(shell, SWT.NONE);
		lblName.setText("名称");
		lblName.setAlignment(SWT.RIGHT);
		lblName.setBounds(328, 130, 100, 20);
		
		txtName = new Text(shell, SWT.BORDER);
		txtName.setTextLimit(100);
		txtName.setBounds(434, 129, 230, 23);
		shell.setTabList(new Control[]{txtName, txtPwd, txtAddress, txtPort, txtDBName, txtUsername, txtPassword, btnSave, btnLoad});
		
		
		//Label lblAddress = ControlUtil.getLabel(shell, "地址", Const.FONT12, 220, 121, 62, 20, SWT.RIGHT);
		
	}
}
